/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import other.Costants;

/**
 *
 * @author alessandro
 */
public class Deck implements Serializable{
    private final int N_CARDS = 52;
    private List<Card> cards = new ArrayList<Card>(0);
    private ArrayList<Card> cardsPlayer1  = new ArrayList<Card>(13);
    private ArrayList<Card> cardsPlayer2  = new ArrayList<Card>(13);
    private ArrayList<Card> cardsPlayer3  = new ArrayList<Card>(13);
    private ArrayList<Card> cardsPlayer4  = new ArrayList<Card>(13);
    private List<Card> cardsOnTable;
    private int token;


    public Deck() {
        this.createDeck();
    }

    private void createDeck() {
        /*prende le 52 carte e le mette in lista*/
        for (int j = 0; j < Costants.SEED.length; j++) {
            for (int x = 0; x < Costants.CARDS.length; x++) {
                if (!(Costants.CARDS[x] == 2 && Costants.SEED[j].equals("F"))) { //il 2 di fiori non viene inserito nel mazzo
                    cards.add(new Card(Costants.SEED[j], Costants.CARDS[x]));
                }
            }
        }

        System.out.println("MAZZO DA " + cards.size());

    }
    public ArrayList<Card> sortPlayerCard(ArrayList playerCards) {
        Collections.sort(playerCards);
        return playerCards;
    }

    public void shuffleCards() {
        Collections.shuffle(cards);
    }

    public void startGivingCards() {
        token = (int)(3*Math.random());
        System.out.println("TOKEN:" + token);
        int NORMAL = 13;
        int START = 0;
        int END = 13;
        Card dueFiori = new Card("F", 2); //2 di fiori

        //algoritmo per assegnare il 2 di fiori
        if (token == 0) {
            cardsPlayer1.add(dueFiori);
            END = END -1;
        }
        cardsPlayer1.addAll(cards.subList(START, END));
        System.out.println("Giocatore 1 n. carte:"+ cardsPlayer1.size());
        System.out.println("Prima carta giocatore 1:"+cardsPlayer1.get(0).getSeed() + cardsPlayer1.get(0).getNumber());
        System.out.println("Ultima carta giocatore 1:"+cardsPlayer1.get(cardsPlayer1.size()-1).getSeed() + cardsPlayer1.get(cardsPlayer1.size()-1).getNumber());

        START = END;
        END = END + NORMAL;

        if (token == 1) {
            cardsPlayer2.add(dueFiori);
            END = END -1;
        }
        cardsPlayer2.addAll(cards.subList(START, END));
        System.out.println("Giocatore 2 n. carte:"+ cardsPlayer2.size());
        System.out.println("Prima carta giocatore 2:"+cardsPlayer2.get(0).getSeed() + cardsPlayer2.get(0).getNumber());
        System.out.println("Ultima carta giocatore 2:"+cardsPlayer2.get(cardsPlayer2.size()-1).getSeed() + cardsPlayer2.get(cardsPlayer2.size()-1).getNumber());

        START = END;
        END = END + NORMAL;

        if (token == 2) {
            cardsPlayer3.add(dueFiori);
            END = END -1;
        }
        cardsPlayer3.addAll(cards.subList(START, END));
        System.out.println("Giocatore 3 n. carte:"+ cardsPlayer3.size());
        System.out.println("Prima carta giocatore 3:"+cardsPlayer3.get(0).getSeed() + cardsPlayer3.get(0).getNumber());
        System.out.println("Ultima carta giocatore 3:"+cardsPlayer3.get(cardsPlayer3.size()-1).getSeed() + cardsPlayer3.get(cardsPlayer3.size()-1).getNumber());

        START = END;
        END = START + NORMAL;

        if (token == 3) {
            cardsPlayer4.add(dueFiori);
            END = END -1;
        }
        cardsPlayer4.addAll(cards.subList(START, END));
        System.out.println("Giocatore 4 n. carte:"+ cardsPlayer4.size());
        System.out.println("Prima carta giocatore 4:"+cardsPlayer4.get(0).getSeed() + cardsPlayer4.get(0).getNumber());
        System.out.println("Ultima carta giocatore 4:"+cardsPlayer4.get(cardsPlayer4.size()-1).getSeed() + cardsPlayer4.get(cardsPlayer4.size()-1).getNumber());

        START = 0;
        END = 13;

        //cardsPlayer1 = this.sortPlayerCard(cardsPlayer1);
        //cardsPlayer2 = this.sortPlayerCard(cardsPlayer2);
        //cardsPlayer3 = this.sortPlayerCard(cardsPlayer3);
        //cardsPlayer4 = this.sortPlayerCard(cardsPlayer4);
    }

    /* ritorna il numero di carte "in mano" al giocatore*/
    public int getCardsNumberInDeck(ArrayList<Card> cards) {
        int number = 0;
        for (int i=0; i < cards.size(); i++){
            if(cards.get(i).getState().equals(Costants.IN_DECK)) {
                number = number + 1;
            }
        }
        return number;
    }

    /* GETTERS AND SETTERS */

    public List<Card> getCardsOnTable() {
        return cardsOnTable;
    }
    public void setCardsOnTable(List<Card> cardsOnTable) {
        this.cardsOnTable = cardsOnTable;
    }
    public List<Card> getCards() {
        return cards;
    }
    public void setCards(List<Card> cards) {
        this.cards = cards;
    }
    public ArrayList<Card> getCardsPlayer1() {
        return cardsPlayer1;
    }
    public void setCardsPlayer1(ArrayList<Card> cardsPlayer1) {
        this.cardsPlayer1 = cardsPlayer1;
    }
    public ArrayList<Card> getCardsPlayer2() {
        return cardsPlayer2;
    }
    public void setCardsPlayer2(ArrayList<Card> cardsPlayer2) {
        this.cardsPlayer2 = cardsPlayer2;
    }
    public ArrayList<Card> getCardsPlayer3() {
        return cardsPlayer3;
    }
    public void setCardsPlayer3(ArrayList<Card> cardsPlayer3) {
        this.cardsPlayer3 = cardsPlayer3;
    }
    public ArrayList<Card> getCardsPlayer4() {
        return cardsPlayer4;
    }
    public void setCardsPlayer4(ArrayList<Card> cardsPlayer4) {
        this.cardsPlayer4 = cardsPlayer4;
    }
    public int getToken() {
        return token;
    }
    public void setToken(int token) {
        if(token == 4)
            token = 0;
        this.token = token;
    }


    

    


}
